package com.sparrow.source.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;

import static com.sparrow.source.common.RabbitConstant.FEIYOU_EXCHANGE_BUS;
import static com.sparrow.source.common.RabbitConstant.PEOPLE_VERIFY_REPORT_QUEUE;
import static com.sparrow.source.common.RabbitConstant.PEOPLE_VERIFY_REPORT_QUEUE_ROUTE_KEY;

/**
 * @author bryceyung
 * @since 2024/11/27
 */
@Configuration
@Slf4j
public class RabbitConfig {

    @Value("${spring.rabbitmq.host}")
    private String host;
    @Value("${spring.rabbitmq.port}")
    private Integer port;
    @Value("${spring.rabbitmq.username}")
    private String userName;
    @Value("${spring.rabbitmq.password}")
    private String password;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setHost(host);
        cachingConnectionFactory.setPort(port);
        cachingConnectionFactory.setUsername(userName);
        cachingConnectionFactory.setPassword(password);
        cachingConnectionFactory.setChannelCheckoutTimeout(10000);
//        cachingConnectionFactory.setExecutor(Executors.newFixedThreadPool(10));

        return cachingConnectionFactory;
    }


    @Bean
    @Autowired
    public RabbitAdmin amqpAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }


    @Bean
    @Autowired
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack) {
                log.error(">>>>>> 消息接收失败，失败原因：[{}] >>>>>>", cause);
            }
        });
        return rabbitTemplate;
    }

    /**
     * 创建交换机
     *
     * @return
     */
    @Bean
    public DirectExchange feiYouDirectExchange() {
        return new DirectExchange(FEIYOU_EXCHANGE_BUS, true, false);
    }

    /**
     * 创建队列
     *
     * @return
     */
    @Bean
    public Queue peopleVerifyQueue() {
        return new Queue(PEOPLE_VERIFY_REPORT_QUEUE);
    }

    /**
     * 绑定
     *
     * @return
     */
    @Bean
    public Binding peoleVerifyBinding(@Qualifier("peopleVerifyQueue") Queue peopleVerifyQueue,
            @Qualifier("feiYouDirectExchange") DirectExchange exchange) {
        return BindingBuilder.bind(peopleVerifyQueue).to(exchange).with(PEOPLE_VERIFY_REPORT_QUEUE_ROUTE_KEY);
    }

}
